﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace AutoCadConvert
{
    public class Stuff : BspObject
    {
        public string PrimitiveID;//sn
        public string PrimitiveType;
        public string PrimitiveCode;//typecode
        public string Rotation;
        public Point MaxBox;
        public Point MinBox;
        public Stuff()
        {
        }
        public override IEnumerable<XElement> ToXml()
        {
            return base.ToXml().Concat(new XElement[] {
                new XElement("PrimitiveID",PrimitiveID),
                new XElement("PrimitiveType",PrimitiveType),
                new XElement("PrimitiveCode",PrimitiveCode),
                new XElement("Rotation",Rotation),
                new XElement("MaxBox",MaxBox.ToMirror().To3DString()),
                new XElement("MinBox",MinBox.ToMirror().To3DString()),
            });
        }
    }

    class StuffNode: Collection
    {
        private List<Stuff> m_stuffNode = new List<Stuff>();
        public StuffNode(House house) : base(house) { }
        public override void AjustLineAndPoint()
        {
            //位置矫正
            Point center = m_house.Item<Points>().m_Center;
            foreach (BspObject obj in m_house.objList)
            {
                Collection collect = obj as Collection;
                if (collect != null)
                {
                    foreach (BspObject item in collect.m_EntityList)
                    {
                        if (item is Block)
                        {
                            Block bref = item as Block;
                            if (bref.m_Block.GetValueAttributesInBlock("图例分类") == "硬装" 
                                && bref.m_Block.GetValueAttributesInBlock("图例编码") != "zhuzi"
                                && bref.m_Block.GetValueAttributesInBlock("图例编码") != "yakou"
                                && bref.m_Block.GetValueAttributesInBlock("图例编码") != "door"
                                && bref.m_Block.GetValueAttributesInBlock("图例编码") != "chuanghu")
                            {
                                List<Point> ptls = new List<Point>();
                                Point basePt = new Point(bref.m_Block.Position);
                                Block.RecurBlockReference(bref.m_Block,bref.m_Block.ScaleFactors,new Point(bref.m_Block.Position),center,ref ptls);//解析数据
                                if (ptls.Count == 0) continue;
                                m_stuffNode.Add(new Stuff()
                                {
                                    PrimitiveID = bref.m_Block.GetValueAttributesInBlock("图例编码"),
                                    PrimitiveType = bref.m_Block.GetValueAttributesInBlock("类型名称"),
                                    PrimitiveCode = bref.m_Block.GetValueAttributesInBlock("类型编号"),
                                    MaxBox = new Point() { X = ptls.Max(o => o.X), Y = ptls.Max(o => o.Y)},
                                    MinBox = new Point() { X = ptls.Min(o => o.X), Y = ptls.Min(o => o.Y) },
                                    Rotation = "x=0,y=" + (int)(bref.m_Block.Rotation * 180 / Math.PI) + ",z=0",
                                });
                            }
                        }
                    }
                }
            }
        }
        public override IEnumerable<XElement> ToXml()
        {
            int idx = 0;
            foreach (Stuff ts in m_stuffNode)
                yield return new XElement("_" + idx++, ts.ToXml());
        }
    }
}
